package com.lesso.yzh.sunflower.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.lesso.yzh.sunflower.core.ApiResponse;
import com.lesso.yzh.sunflower.pojo.dto.AttendanceRuleDTO;
import com.lesso.yzh.sunflower.pojo.dto.AttendanceRuleQueryDTO;
import com.lesso.yzh.sunflower.pojo.dto.AttendanceRuleUpdateDTO;
import com.lesso.yzh.sunflower.pojo.vo.AttendanceRuleVO;
import com.lesso.yzh.sunflower.service.IAttendanceRuleService;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/base/attendanceRule")
public class AttendanceRuleController {
    @DubboReference
    private IAttendanceRuleService attendanceRuleService;

    @PostMapping("/save")
    public ApiResponse<Integer> save(@RequestBody AttendanceRuleDTO dto) {
        Assert.notNull(dto.getRuleName(), "名字不能为空");
        Assert.notNull(dto.getRuleCode(), "规则编号不能为空");

        return ApiResponse.ok("success", attendanceRuleService.save(dto));
    }

    @PutMapping("/update")
    public ApiResponse<Integer> update(@RequestBody AttendanceRuleUpdateDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.update(dto));
    }

    @GetMapping("/page")
    public ApiResponse<Page<AttendanceRuleVO>> page(AttendanceRuleQueryDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.page(dto));
    }

    @GetMapping("/custom/page")
    public ApiResponse<Page<AttendanceRuleVO>> customPage(AttendanceRuleQueryDTO dto) {
        return ApiResponse.ok("success", attendanceRuleService.customPage(dto));
    }

    @DeleteMapping("/delete")
    public ApiResponse<Object> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "id不能为空");
        if (id < 0) {
            throw new RuntimeException("id不能小于0");
        }
        attendanceRuleService.deleteById(id);
        return ApiResponse.ok();
    }
}
